#pragma once
#include "common.h"
#include "ThreadCache.h"
#include "PageCache.h"

static ObjectPool<TreadCache> ThreadCachePool;
static void *ConcurrentAlloc(size_t size)
{
    if (size > MAXBITYS)
    {
        int alignNum = SizeClass::RoundUp(size);

        PageCache::Lock();
        Span *span = PageCache::GetInstance()->NewSpan(alignNum >> ShiftPageSize);
        span->_size = alignNum;
        span->_isUse = true;
        PageCache::UnLock();

        return (void *)(span->_PageId << ShiftPageSize);
    }
    if (pTLSThreadCache == nullptr)
    {
        // pTLSThreadCache = new TreadCache();
        pTLSThreadCache = ThreadCachePool.New();
    }

    return pTLSThreadCache->Allocate(size);
}
static void ConcurrentDelloc(void *obj)
{

    PageId_t id = (PageId_t)obj >> ShiftPageSize;

    PageCache::Lock();
    Span *span = PageCache::GetInstance()->MapObjToSpan(obj);
    PageCache::UnLock();

    size_t size = span->_size;
    if(size > MAXBITYS)
    {
        PageCache::Lock();
        PageCache::GetInstance()->SpnaGiveBackPage(span);
        PageCache::UnLock();
        return;
    }
    assert(pTLSThreadCache);
    pTLSThreadCache->Dellocate(obj, size);
}
